package edu.chinasoft.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import edu.chinasoft.domain.Function;
import edu.chinasoft.service.FunctionService;

/**
 *
 * @author mhao
 * @date 2020年2月12日:下午3:56:48
 * @version V1.0
 * @Description: 
*/
@WebServlet(name = "myDelectFunctionServlet", value = "/servlet/delectfunctionservlet")
public class DelectFunctionServlet extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(DelectFunctionServlet.class);
	private FunctionService functionService;
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" DelectFunctionServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		FunctionService functionServiceImp = wac.getBean("functionService", FunctionService.class);
		this.functionService=functionServiceImp;
		//获取当前权限的id
		String num = request.getParameter("id");
		int id = Integer.parseInt(num);
		Function function = new Function();
		function.setFunctionId(id);
		//调用service的方法删除该权限
		boolean result = functionService.deleteFunctionById(function);
		if(result) {//删除成功
			response.sendRedirect(""+request.getContextPath()+"/servlet/showfunctionservlet");
		}else {//删除失败
			response.sendRedirect(""+request.getContextPath()+"/deletefunctionerror.jsp");
		}
		logger.info(" DelectFunctionServlet >doGet end.......");
	}

	/**
	 *
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info(" DelectFunctionServlet >doPost start.......");
		this.doGet(request, response);
		logger.info(" DelectFunctionServlet >doPost end.......");
	}

}